package com.txhy.certificatechain.ui.activity.user;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ninetripods.aopermission.permissionlib.annotation.NeedPermission;
import com.orhanobut.logger.Logger;
import com.txhy.certificatechain.R;
import com.txhy.certificatechain.presenter.user.RegisterPresenter;
import com.txhy.certificatechain.ui.activity.base.BaseActivity;
import com.txhy.certificatechain.ui.view.user.RegistView;
import com.txhy.certificatechain.utils.CommUtils;
import com.txhy.certificatechain.utils.CountDownTimerUtil;
import com.txhy.certificatechain.utils.ToastUtil;
import com.txhy.certificatechain.utils.UIUtils;

import butterknife.BindView;
import butterknife.OnClick;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: RegisterActivity.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 用户注册
 * 创建时间: 2018/9/20 17:19
 * 更新时间：2018/9/20 17:19
 * -----------------------------------------------------------------
 * 2018/9/20 17:19 : 创建 RegisterActivity.java (侯建军);
 * -----------------------------------------------------------------
 */
public class RegisterActivity extends BaseActivity<RegistView, RegisterPresenter> implements RegistView {

    @BindView(R.id.im_close)
    ImageView im_close;

    @BindView(R.id.ed_phone)
    EditText ed_phone;

    @BindView(R.id.tv_sendCode)
    TextView tv_sendCode;

    @BindView(R.id.ed_code)
    EditText ed_code;

    @BindView(R.id.tv_register)
    TextView tv_register;
    @BindView(R.id.ll_aggre)
    LinearLayout ll_aggre;

    @BindView(R.id.im_aggre)
    ImageView im_aggre;

    @BindView(R.id.tv_text)
    TextView tv_text;
    @BindView(R.id.tv_login)
    TextView tv_login;
    @BindView(R.id.img_logo)
    ImageView img_logo;
    private boolean imgSelect;
    private CountDownTimerUtil mCountDownTimerUtils;

    @Override
    protected RegisterPresenter createPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected int provideContentViewId() {
        //设置为无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置为全屏模式
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        super.initView();
        tv_sendCode.setClickable(false); //设置不可点击
    }
    /**
     * 初始化监听
     */
    @Override
    public void initListener() {
        super.initListener();
        ed_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            /**
             * 文本改变后
             * @param s
             */
            @Override
            public void afterTextChanged(Editable s) {
                int num = (ed_phone.getText() + "").length();
                if (num == 11) {
                    tv_sendCode.setBackground(UIUtils.getResource().getDrawable(R.drawable.editbg_select));
                    tv_sendCode.setTextColor(UIUtils.getResource().getColor(R.color.ed584c));
                    tv_sendCode.setClickable(true); //
                } else {
                    tv_sendCode.setBackground(UIUtils.getResource().getDrawable(R.drawable.edit_bg));
                    tv_sendCode.setTextColor(UIUtils.getResource().getColor(R.color.textColor));
                    tv_sendCode.setClickable(false); //设置不可点击
                }
            }
        });
    }

    /**
     * 事件
     * @param view
     */
    @OnClick({R.id.im_close, R.id.tv_register, R.id.tv_sendCode, R.id.ll_aggre, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //关闭系统
            case R.id.im_close:
                finish();
                break;
            //获取验证码
            case R.id.tv_sendCode:
                sendPhoneCode();
                break;
            //立即注册
            case R.id.tv_register:
                String phone = ed_phone.getText().toString().trim();
                String code = ed_code.getText().toString().trim();
                if (phone.length() != 11) {
                    ToastUtil.showShort(RegisterActivity.this, UIUtils.getString(R.string.ninshuru));
                } else if (TextUtils.isEmpty(code)) {
                    ToastUtil.showShort(RegisterActivity.this, UIUtils.getString(R.string.liangcishuyan));
                } else if (!imgSelect) {
                    ToastUtil.showShort(RegisterActivity.this, UIUtils.getString(R.string.qingxuanze));
                } else {
                    mPresenter.toRegist(phone, CommUtils.getIMEI(RegisterActivity.this), code);
                }
                break;
             //接受交易币协议
            case R.id.ll_aggre:
                if (imgSelect) {
                    im_aggre.setImageResource(R.mipmap.select111);
                    imgSelect = false;
                } else {
                    im_aggre.setImageResource(R.mipmap.select222);
                    imgSelect = true;
                }
                break;
            //登录
            case R.id.tv_login:
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
                break;
        }
    }

    /**
     * 发送手机验证码
     */
    @NeedPermission(value = {android.Manifest.permission.READ_PHONE_STATE}, requestCode = 0)
    private void sendPhoneCode() {
        /**
         * 发送手机号与手机串码
         */
        mPresenter.SendCode(ed_phone.getText().toString().trim(), CommUtils.getIMEI(RegisterActivity.this));
        img_logo.setVisibility(View.GONE);
        Logger.d("hello");
    }

    //接口返回的
    @Override
    public void showProgress(String tipString) {
        showWaitingDialog(tipString);
    }

    @Override
    public void hideProgress() {
        hideWaitingDialog();
    }

    /**
     * 注册成功
     * @param useId
     */
    @Override
    public void registerSuccess(String useId) {
        Intent intent = new Intent(RegisterActivity.this, UploadCardIDActivity.class);
        intent.putExtra("userId", useId);
        startActivity(new Intent(intent));
        finish();
    }

    @Override
    public void registerFail(String msg) {
        ToastUtil.showShort(RegisterActivity.this, msg);
    }

    /**
     * 短信发送成功
     * @param msg
     */
    @Override
    public void sendCodeSuccess(String msg) {
        mCountDownTimerUtils = new CountDownTimerUtil(tv_sendCode, 60000, 1000);
        mCountDownTimerUtils.start();
        ToastUtil.showLong(RegisterActivity.this, "发送验证码成功");
    }

    /**
     * 短信发送失败
     * @param msg
     */
    @Override
    public void sendCodeFail(String msg) {
        ToastUtil.showShort(RegisterActivity.this, msg);
    }
}
